package com.hxzy.framework.web.security;

import com.hxzy.common.constant.UserConstants;
import com.hxzy.common.core.domain.entity.SysUser;
import com.hxzy.common.core.domain.model.LoginUser;
import com.hxzy.common.enums.UserStatus;
import com.hxzy.common.exception.ServiceException;
import com.hxzy.common.utils.StringUtils;
import com.hxzy.system.service.ISysMenuService;
import com.hxzy.system.service.ISysRoleService;
import com.hxzy.system.service.ISysUserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;

/**
 * spring security 数据库查询用户信息用的
 * 1)、根据用户查询sys_user表中的信息
 * 2)、判断状态是否禁用，抛出异常
 *
 *
 * 3)、查询用户拥有的角色权限(sys_role(role_key)  关联   sys_user_role) )         Collection<? extends GrantedAuthority> getAuthorities();
 * 4)、查询角色拥有的权限 ( sys_user_role  . sys_role , sys_role_menu,  sys_menu[perms])  Collection<? extends GrantedAuthority> getAuthorities();
 * 5)、把上面所有的数据，组装到 UserDetails接口实现类LoginUser里面
 */
@Log4j2
@Component
public class TzxmUserDetailServiceImpl implements UserDetailsService {

    /**
     * 用户
     */
    @Autowired
    private ISysUserService  sysUserService;
    /**
     * 角色
     */
    @Autowired
    private ISysRoleService  sysRoleService;

    /**
     * 菜单
     */
    @Autowired
    private ISysMenuService sysMenuService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser  sysUser=this.sysUserService.selectUserByUserName(username);
        if (StringUtils.isNull(sysUser))
        {
            log.info("登录用户：{} 不存在.", username);
            throw new ServiceException("登录用户：" + username + " 不存在");
        }

        if (UserStatus.DELETED.getCode().equals(sysUser.getDelFlag()))
        {
            log.info("登录用户：{} 已被删除.", username);
            throw new ServiceException("对不起，您的账号：" + username + " 已被删除");
        }

        if (UserStatus.DISABLE.getCode().equals(sysUser.getStatus()))
        {
            log.info("登录用户：{} 已被停用.", username);
            throw new ServiceException("对不起，您的账号：" + username + " 已停用");
        }

        //查询用户拥有的角色和菜单权限
        Set<String> roleSet=this.sysRoleService.selectRolePermissionByUserId(sysUser.getUserId());

        //判断是不是管理员
        boolean isSuper=roleSet.contains(UserConstants.SUPER_MAN_NAME);

       //查询用户的角色拥有的菜单权限

        Set<String> menuSet=new HashSet<>();
        if(isSuper){
            menuSet.add("*:*:*");
        }else{
            menuSet=this.sysMenuService.selectMenuPermsByUserId(sysUser.getUserId());
        }

        //返回UserDetail接口的实现类
        LoginUser  loginUser=new LoginUser();
        loginUser.setUser(sysUser);
        loginUser.setUserId(sysUser.getUserId());
        loginUser.setDeptId(sysUser.getDeptId());
        //只关心，不关心角色
        loginUser.setPermissions(menuSet);

        return loginUser;
    }
}
